//
//  MovieViewController.m
//
//  Created by 陈萍 on 15/11/10.
//  Copyright © 2015年 陈萍. All rights reserved.
//

#import "MovieViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "LiveItem.h"

@interface MovieViewController ()
@property(nonatomic,strong)AVPlayer *player;
@property (weak, nonatomic) IBOutlet UIView *movieView;
//遮罩层
@property (weak, nonatomic) IBOutlet UIView *shadeView;

@property(nonatomic,strong)UIView *ControlView;
@property(nonatomic,assign)BOOL isPause;
@property(nonatomic,strong)UIButton *pauseButton;
@property(nonatomic,strong)UIButton *backButton;
@property(nonatomic,strong)UILabel *titleLabel;

@property(nonatomic,strong)AVPlayerLayer *playerLayer;

#define screenFrame [UIScreen mainScreen].bounds
@end

@implementation MovieViewController

- (void)viewDidLoad {

    [super viewDidLoad];
            
    self.movieView.backgroundColor = [UIColor blackColor];
    //网络
    NSURL *playURl = [NSURL URLWithString:_playItem.url];
//    NSURL *playURl = [NSURL URLWithString:@"http://106.120.175.80:55336/tslive/c23_ct_hljtv1_hljws_smooth_t10/c23_ct_hljtv1_hljws_smooth_t10.m3u8"];
    AVAsset *asset = [AVAsset assetWithURL:playURl];
    AVPlayerItem *item = [AVPlayerItem playerItemWithAsset:asset];
    _player = [AVPlayer playerWithPlayerItem:item];
    _playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
    _playerLayer.frame = CGRectMake(0, 0, screenFrame.size.height, screenFrame.size.width);
    _shadeView.frame = CGRectMake(0, 0, screenFrame.size.height, screenFrame.size.width);
    _playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    [self.movieView.layer addSublayer:_playerLayer];
    [_player.currentItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];//监听属性
    [_player.currentItem addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:nil];
    [self setUpControllView];
    _ControlView.hidden = 1;
    _shadeView.hidden = 1;
    
    [MBProgressHUD showMessage:@""];

}

-(void)setUpControllView{
    _ControlView = [[UIView alloc]init];
    _player.volume = 2;
    _ControlView.frame = CGRectMake(0, 0, screenFrame.size.height, screenFrame.size.width);
    
    [self.view addSubview:_ControlView];
 
    //返回button
    _backButton = [[UIButton alloc] init];
    _backButton.frame = CGRectMake(20, 0, 350,50);
    [_backButton addTarget:self action:@selector(backButtonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    [_ControlView addSubview:_backButton];
    
    UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"back.png"]];
    image.frame = CGRectMake(0, 10, 20, 20);
    [_backButton addSubview:image];
    //__titleLabel
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.frame = CGRectMake(20, 10, 400, 20);
    _titleLabel.font = [UIFont systemFontOfSize:18];
    _titleLabel.textColor = [UIColor whiteColor];
    _titleLabel.text = _playItem.title;
    [_backButton addSubview:_titleLabel];

    //暂停button
    _pauseButton = [[UIButton alloc] init];
    [_pauseButton setImage:[UIImage imageNamed:@"暂停白.png"] forState:UIControlStateNormal];
    _pauseButton.frame = CGRectMake(screenFrame.size.height/2-22, screenFrame.size.width/2-22, 44, 44);
    [_pauseButton addTarget:self action:@selector(pauseButtonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    [_ControlView addSubview:_pauseButton];

    //在控件层加手势
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGRAction:)];
    //需要手指的个数
    tapGR.numberOfTouchesRequired = 1;
    [_ControlView addGestureRecognizer:tapGR];
    //在视频层加手势
    UITapGestureRecognizer *movietapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(movietapGRAction:)];
    movietapGR.numberOfTouchesRequired = 1;
    [_movieView addGestureRecognizer:movietapGR];
    

}
//在控件层加手势
- (void)tapGRAction:(UITapGestureRecognizer *)sender{
    _ControlView.hidden = 1;
    _shadeView.hidden = 1;
}
//在视频层加手势
- (void)movietapGRAction:(UITapGestureRecognizer *)sender{
    _ControlView.hidden = 0;
    _shadeView.hidden = 0;
}


//返回Button点击事件
-(void)backButtonDidClicked:(UIButton *)sender{
    [self dismissViewControllerAnimated:YES completion:^{
        NSLog(@"Back");
    }];
}

//暂停Button点击事件
-(void)pauseButtonDidClicked:(UIButton *)sender{
    [sender setShowsTouchWhenHighlighted:1];
    if (!_isPause) {
        [_pauseButton setImage:[UIImage imageNamed:@"播放白.png"] forState:UIControlStateNormal];
        [_player pause];
        _isPause = 1;

    }else{
        [_pauseButton setImage:[UIImage imageNamed:@"暂停白.png"] forState:UIControlStateNormal];
        [_player play];
        _isPause = 0;
    }
}


//监听player属性
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    AVPlayerItem *playerItem = (AVPlayerItem *)object;
    if ([keyPath isEqualToString:@"status"]) {
        if ([playerItem status] == AVPlayerStatusReadyToPlay) {
            NSLog(@"AVPlayerStatusReadyToPlay");
            if( [UIApplication sharedApplication].applicationState == UIApplicationStateActive ||  [UIApplication sharedApplication].applicationState == UIApplicationStateInactive) {
                [MBProgressHUD hideHUD];
                [self.player play];
            }
            else {
                [self.player pause];
            }
            _ControlView.hidden = 1;
            _shadeView.hidden = 1;
            
        } else if ([playerItem status] == AVPlayerStatusFailed) {
            NSLog(@"AVPlayerStatusFailed");
        }
    }
}


-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskLandscape;
}



-(void)viewWillDisappear:(BOOL)animated{
    [_player pause];
    [_player setRate:0];
    [_player.currentItem removeObserver:self forKeyPath:@"loadedTimeRanges" context:nil];
    [_player.currentItem removeObserver:self forKeyPath:@"status" context:nil];
    [_player replaceCurrentItemWithPlayerItem:nil];
    self.player = nil;
}

@end
